package org.bizming.web.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.bizming.base.BaseErrCode;
import org.bizming.base.BaseException;
import org.bizming.base.RecordConstants;
import org.bizming.record.model.RecordType;
import org.bizming.record.service.RecordTypeService;
import org.bizming.util.DateUtil;
import org.bizming.web.model.ext.RecordTypeExt;
import org.bizming.web.util.WebUtil;

import com.alibaba.fastjson.JSONObject;

public class RecordTypeAction {

	private RecordTypeService recordTypeService;

	private int page = 1;
	private int pages = RecordConstants.LIMIT;

	private RecordType type;

	/**
	 * 进入单个页面,包括更新,新建
	 * 
	 * @return
	 */
	public String viewType() {
		RecordType recordType = null;
		try {
			if (type != null && type.getId() != null) {
				recordType = recordTypeService.get(type.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebUtil.getRequest().setAttribute("type", recordType);
		return "view";
	}

	/**
	 * 修改记录类型
	 * 
	 * @return
	 */
	public String updateOrAddType() {
		int errCode = BaseErrCode.SUCCESS;

		try {
			if (type != null && type.getId() != null) {
				recordTypeService.update(type);
			} else {
				recordTypeService.add(type);
			}

			return selectList();
		} catch (BaseException e) {
			errCode = e.getErrCode();
		} catch (Exception e) {
			e.printStackTrace();
			errCode = BaseErrCode.UNKNOWN;
		}
		WebUtil.getRequest().setAttribute("type", type);
		WebUtil.getRequest().setAttribute("errCode", errCode);

		return "view";
	}

	/**
	 * 删除记录类型
	 * 
	 * @return
	 */
	public String removeType() {
		int errCode = BaseErrCode.SUCCESS;
		try {
			boolean remove = recordTypeService.remove(type.getId());
			if (remove) {

			} else {
				throw new BaseException(BaseErrCode.UNKNOWN);
			}
			WebUtil.getResponse().sendRedirect("recordType!selectList.action");
			return null;
		} catch (BaseException e) {
			e.printStackTrace();
			errCode = e.getErrCode();
		} catch (Exception e) {
			e.printStackTrace();
			errCode = BaseErrCode.UNKNOWN;
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", errCode);

		// WebUtil.outputJSONResult(jsonObject, WebUtil.getResponse());

		return "list";
	}

	/**
	 * 显示记录类型
	 * 
	 * @return
	 */
	public String selectList() {

		List<RecordType> typeList = recordTypeService.selectList(page, pages);
		List<RecordTypeExt> exts = new ArrayList<RecordTypeExt>();
		if (typeList != null) {
			Calendar c = Calendar.getInstance();
			for (RecordType type : typeList) {
				RecordTypeExt ext = new RecordTypeExt();
				ext.setCreateTime(DateUtil.convert(type.getCreateTime(), c));

				ext.setType(type);
				exts.add(ext);
			}
		}

		int count = recordTypeService.count();

		WebUtil.getRequest().setAttribute("typeList", exts);
		WebUtil.getRequest().setAttribute("count", count);

		return "list";
	}

	// ===========================================
	public RecordTypeService getRecordTypeService() {
		return recordTypeService;
	}

	public void setRecordTypeService(RecordTypeService recordTypeService) {
		this.recordTypeService = recordTypeService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public RecordType getType() {
		return type;
	}

	public void setType(RecordType type) {
		this.type = type;
	}

}
